package com.pxl.controller.teacher;

import com.pxl.entity.Course;
import com.pxl.entity.Student;
import com.pxl.entity.Teacher;
import com.pxl.service.CourseService;
import com.pxl.service.PageService;
import com.pxl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private UserService userService;

    @Autowired
    private PageService pageService;

    @Autowired
    private CourseService courseService;

    @RequestMapping("/teacherIndex")
    public String teacherIndex() {
        return "teacher/teacherIndex";
    }

    @RequestMapping("/teacherInfo")
    public String teacherInfo(@RequestParam("teaId") int teaId, Model model, HttpServletRequest request) {

        model.addAttribute("teacherInfo",userService.selectTeaById((Integer) request.getSession().getAttribute("teaid")));
        return "teacher/teacherInfo";
    }

    /**
     *  编辑密码
     * @return
     */
    @RequestMapping("/editTeaPass")
    public String editTeaPass(){
        return "teacher/editTeaPass";
    }

    /**
     *  修改密码
     * @return
     */
    @RequestMapping("/changeTeaPass")
    public String changeTeaPass(@RequestParam("prePass") String prePass, @RequestParam("firstPass")String nowPass, @RequestParam("id") int teaId,Model model, HttpServletRequest request) {

        // 拿到用户的 id ,根据 id 进行修改用户的 密码
//        int teaId = (int) request.getSession().getAttribute("teaId");
        System.out.println(teaId);
        System.out.println(".....................................................................");

        // 如果原密码不对,则让用户重新输入
        if(userService.checkAccount(teaId,prePass) == 0) {
            model.addAttribute("msg","原密码错误!");
            return "teacher/editTeaPass";
        } else {
            Teacher teacher = new Teacher();
            teacher.setTeaId(teaId);

            teacher.setTeaPass(nowPass);

            userService.changeTeaPass(teacher);
            model.addAttribute("teacherInfo",userService.selectTeaById(teaId));
            return "teacher/teacherInfo";
        }

    }

    /**
     *  课程信息列表
     * @param page
     * @param model
     * @param request
     * @return
     */

    @RequestMapping("/courseList")
    public String courseList(@RequestParam("page") int page,Model model,HttpServletRequest request) {

        System.out.println("....................................");
        model.addAttribute("paging",pageService.subList(page,courseService.queryAllById((int) request.getSession().getAttribute("teaid"))));

        return "teacher/courseList";
    }

    @RequestMapping("/insertCourse")
    public String insertCourse(Model model,HttpServletRequest request) {
        model.addAttribute("insList",courseService.queryAllIns());
        return "teacher/insertCourse";
    }

    // 添加新课程成功
    @RequestMapping("/insertCourseSuccess")
    public String insertCourseSuccess(@RequestParam("content") String content, @RequestParam("page") int page, Model model,HttpServletRequest request) throws UnsupportedEncodingException {
        // 解析传到后台的 content 内容
        String[] det = URLDecoder.decode(URLDecoder.decode(content, "utf-8"), "utf-8").split("\\|");

        System.out.println(det.length);
        // 获取到课程的 id 同时添加课程
        int courseId = courseService.insertCourse(det[0],det[1],(int)request.getSession().getAttribute("teaid"));

        System.out.println(courseId);

        courseService.insertInsLimit(det[2],courseId);

        model.addAttribute("paging",pageService.subList(page,courseService.queryAllById((int) request.getSession().getAttribute("teaid"))));

        return "teacher/courseList";
    }

    /**
     *  修改课程的相关信息(提供信息准备传给后台)
     * @param classId
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/updateCourse")
    public String updateCourse(@RequestParam("classId") int classId,Model model, HttpServletRequest request) {

        model.addAttribute("courseInfo",courseService.queryCourseInfoById(classId));
        model.addAttribute("insList",courseService.queryAllIns());
        //  为了得到老师没有修改之前所选的学院.然后进行修改
        model.addAttribute("checkIns",courseService.selectCourseLimit(classId));

        return "teacher/editCourse";
    }


    /**
     *  修改信息传给后台进行更新
     * @param content
     * @param page
     * @param model
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */

    @RequestMapping("/updateCourseSuccess")
    public String updateCourseSuccess(@RequestParam("content") String content,@RequestParam("page") int page,@RequestParam("classId") int classId, Model model,HttpServletRequest request) throws UnsupportedEncodingException {
        String[] det = URLDecoder.decode(URLDecoder.decode(content, "utf-8"), "utf-8").split("\\|");
        System.out.println(det[0] + " " + det[1] + " " + det[2]);

        // 根据老师的 id 进行修改相关的课程信息
        int courseId = courseService.updateCourse(det[0],det[1],(int)request.getSession().getAttribute("teaid"),classId);

        // 更新课程的 学院限制信息
        courseService.updateInsLimit(det[2],courseId);

        model.addAttribute("paging",pageService.subList(page,courseService.queryAllById((int)request.getSession().getAttribute("teaid"))));


        return "teacher/courseList";

    }

    /**
     *  删除老师增加的课程
     * @param classId
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/deleteCourse")
    public String deleteCourse(@RequestParam("courseId") int classId,Model model ,HttpServletRequest request) {
        courseService.deleteCourse(classId);
        model.addAttribute("paging",pageService.subList(1,courseService.queryAllById((int)request.getSession().getAttribute("teaid"))));
        return "teacher/courseList";
    }


    /**
     *  教师的课程管理页面(评分和删除课程)
     * @param classId
     * @param page
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/detailCourse")
    public String detailCourse(@RequestParam("courseId") int classId, @RequestParam("page") int page,Model model ,HttpServletRequest request)  {

        System.out.println(courseService.queryStuByCourseId(classId));
        model.addAttribute("paging",pageService.subList(page,courseService.queryStuByCourseId(classId)));

        return "teacher/detailCourse";
    }

    /**
     *  更新评分
     * @param classId
     * @param stuId
     * @param score
     * @param model
     * @param page
     * @return
     */
    @RequestMapping("/updateScore")
    public String updateScore(@RequestParam("courseId") int classId,@RequestParam("stuId") int stuId,@RequestParam("score")int score,Model model,@RequestParam("page") int page) {
        courseService.updateScore(classId,stuId,score);
        model.addAttribute("paging",pageService.subList(page,courseService.queryStuByCourseId(classId)));
        return "teacher/detailCourse";
    }

    /**
     *  删除学生的课程
     * @param classId
     * @param stuId
     * @param model
     * @return
     */
    @RequestMapping("/deleteStuCourse")
    public String deleteStuCourse(@RequestParam("courseId")int classId,@RequestParam("stuId") int stuId,Model model) {

        courseService.deleteStuCourse(classId,stuId);

        model.addAttribute("paging",pageService.subList(1,courseService.queryStuByCourseId(classId)));
        return "teacher/detailCourse";
    }
}
